/* File: run_stacked_DiD_noatc3.do
 * Author: Luca Maini
 * Purpose: this file runs the script for the stacked DiD analysis in the paper
 *
 */



*** Define program to create Regression Tables and Figures

/* 	This program runs regressions for a list of dependent variables on a list 
	of merger event indicators. It also allows for a few pre-specified sample 
	restrictions and weights, which are occasionally used in robustness checks.
	
	Variables:
	
	table_tag --> identifies table number in the paper
	yList --> identifies dependent variables
	xList --> identifies event-study control variables
	sample --> identifies sample restrictions (ALL/FHM string)
	weights --> identifies weights in the regression (YES/NO string)
	graphs --> indicator for running the graph code or not
	
*/

capture clear programs
program define mergerPaperFigure
	
	args figure_tag data yVar sample span graph
	
	* select sample
	
	/*	Steps:
		
		1. Select sample of treated drugs (get a list by Product and year)
		2. Match to matched_cohort data
		3. set up the data for the stacked DiD
		4. Run the stacked DiD
		
	*/
	
	use "${coeffdir}\coefficients_`data'_final_noatc3.dta", clear
	keep if `sample'
	keep Product year cem_varlist
	
	rename Product Treated_Product
	rename year yearAcq
	
	* merge 
	merge 1:m Treated_Product yearAcq cem_varlist using "${maindir}\matched_cohorts_`data'_noatc3.dta", keep(match)
	
	order Treated_Product yearAcq year Product
	sort Treated_Product yearAcq year Product
	
	* Now set up the data for the stacked DiD
	gen t0 = yearAcq
	gen t = year - yearAcq
	keep if t >= -`span' & t <= `span'
	replace t = t + `span' + 1	// to avoid negative values
	
	gen treatedt = t * (treated == 1)
	gen controlt = t * (treated == 0)
	egen cohort = group(Treated_Product yearAcq)
	
	*** Graph: Balanced T, with FEs to control for panel and isolate year of acq.
	areg `yVar' ib`span'.t ib`span'.treatedt, absorb(Product) cluster(Product)
	
	local N = `span' * 2 + 1
	forvalues i = 1/`N' {
		capture local b_`i'_1 = _b[`i'.treatedt]	// we use capture because in some 
													// robustness checks we have very small 
													// sample sizes that do not not cover 
													// all years of data
		capture local se_`i'_1 = _se[`i'.treatedt]
		}
	
	clear
	set obs `N'
	
	gen diff = .
	gen diff_se = .
	
	gen t = _n - `span' - 1
	
	forvalues i = 1/`N' {
		capture replace diff = `b_`i'_1' in `i'
		capture replace diff_se = `se_`i'_1' in `i'
		}
	
	gen diff_lb = diff - 1.96*diff_se
	gen diff_ub = diff + 1.96*diff_se
	
	* save coefficients
	save "${coeffdir}\stacked-DiD\\`figure_tag'_noatc3.dta", replace
		
	* Graph
	if "`graph'" == "YES" {
	graph set window fontface "${paperFont}"
		twoway	(connect diff t, ///
					color(gs0) msymbol(d)) ///
				(rspike diff_lb diff_ub t, ///
					lcolor(gs0)), ///
			xtitle("Years from acquisition", height(5)) xlabel(-`span'(1)`span') ///
			ytitle("Change relative to year prior to acquisition") ///
			ylabel(#5, format(%4.1f)) xline(0, lcolor(gs10%50) lwidth(*15)) ///
			legend(off) name(diff, replace)
		graph export "${paperdir}\Figures\\`figure_tag'.pdf", as(pdf) replace
		}	
end




////////////////////////////////////////////
////									////
////	APPENDIX D. Robustness checks	////
////									////
////////////////////////////////////////////

*** Excluding drugs without ATC-3 from the control group

* Effect of horizontal acquisitions on net price
mergerPaperFigure "Figure OA7a"	/// figure tag
				  "sales" /// data
				  "w_log_net"	/// yVar
				  "valeant_ind == 0 & overlap == 1"	/// sample selection rules
				  "3"	/// span
				  "YES"	// graph

* Split effect (HSR) of horizontal acquisitions on net price
mergerPaperFigure "Figure OA8a1"	/// figure tag
				  "sales" /// data
				  "w_log_net"	/// yVar
				  "(valeant_ind == 0 & overlap_stealth == 1)"	/// sample selection rules
				  "3"	/// span
				  "NO"	// graph
mergerPaperFigure "Figure OA8a2"	/// figure tag
				  "sales" /// data
				  "w_log_net"	/// yVar
				  "(valeant_ind == 0 & overlap_nonstealth == 1)"	/// sample selection rules
				  "3"	/// span
				  "NO"	// graph

use "${coeffdir}\stacked-DiD\Figure OA8a1_noatc3.dta", clear
rename diff* below_diff*
replace t = t - 0.025
merge 1:1 t using "${coeffdir}\stacked-DiD\Figure OA8a2_noatc3.dta"
replace t = t + 0.025 if _merge == 2

twoway	(connect below_diff diff t, ///
			color(gs0 gs0) lpattern(solid dash) msymbol(O S)) ///
		(rspike below_diff_lb below_diff_ub t, ///
			lcolor(gs0)) ///
		(rspike diff_lb diff_ub t, ///
			lcolor(gs0)), ///
	xtitle("Years from acquisition", height(5)) xlabel(-3(1)3) ///
	ytitle("Change relative to year prior to acquisition") ///
	ylabel(#5, format(%4.1f)) xline(0, lcolor(gs10%50) lwidth(*15)) ///
	legend(order(2 "Horizontal acquisitions" "above HSR threshold" ///
				 1 "Horizontal acquisitions" "below HSR threshold") cols(1) ///
			   ring(0) bplacement(11) region(color(none)))
graph export "${paperdir}\Figures\Figure OA8a.pdf", as(pdf) replace

* Split effect (HSR) of horizontal acquisitions on volume
mergerPaperFigure "Figure OA9a1"	/// figure tag
				  "sales" /// data
				  "log_units"	/// yVar
				  "(valeant_ind == 0 & overlap_stealth == 1)"	/// sample selection rules
				  "3"	/// span
				  "NO"	// graph
mergerPaperFigure "Figure OA9a2"	/// figure tag
				  "sales" /// data
				  "log_units"	/// yVar
				  "(valeant_ind == 0 & overlap_nonstealth == 1)"	/// sample selection rules
				  "3"	/// span
				  "NO"	// graph

use "${coeffdir}\stacked-DiD\Figure OA9a1_noatc3.dta", clear
rename diff* below_diff*
replace t = t - 0.025
merge 1:1 t using "${coeffdir}\stacked-DiD\Figure OA9a2_noatc3.dta"
replace t = t + 0.025 if _merge == 2

twoway	(connect below_diff diff t, ///
			color(gs0 gs0) lpattern(solid dash) msymbol(O S)) ///
		(rspike below_diff_lb below_diff_ub t, ///
			lcolor(gs0)) ///
		(rspike diff_lb diff_ub t, ///
			lcolor(gs0)), ///
	xtitle("Years from acquisition", height(5)) xlabel(-3(1)3) ///
	ytitle("Change relative to year prior to acquisition") ///
	ylabel(#5, format(%4.1f)) xline(0, lcolor(gs10%50) lwidth(*15)) ///
	legend(order(2 "Horizontal acquisitions" "above HSR threshold" ///
				 1 "Horizontal acquisitions" "below HSR threshold") cols(1) ///
			   ring(0) bplacement(2) region(color(none)))
graph export "${paperdir}\Figures\Figure OA9a.pdf", as(pdf) replace

* Split effect (HSR) of horizontal acquisitions on formulary coverage
mergerPaperFigure "Figure OA9b1"	/// figure tag
				  "coverage" /// data
				  "glp"	/// yVar
				  "(valeant_ind == 0 & overlap_stealth == 1)"	/// sample selection rules
				  "3"	/// span
				  "NO"	// graph
mergerPaperFigure "Figure OA9b2"	/// figure tag
				  "coverage" /// data
				  "glp"	/// yVar
				  "(valeant_ind == 0 & overlap_nonstealth == 1)"	/// sample selection rules
				  "3"	/// span
				  "NO"	// graph

use "${coeffdir}\stacked-DiD\Figure OA9b1_noatc3.dta", clear
rename diff* below_diff*
replace t = t - 0.025
merge 1:1 t using "${coeffdir}\stacked-DiD\Figure OA9b2_noatc3.dta"
replace t = t + 0.025 if _merge == 2

twoway	(connect below_diff diff t, ///
			color(gs0 gs0) lpattern(solid dash) msymbol(O S)) ///
		(rspike below_diff_lb below_diff_ub t, ///
			lcolor(gs0)) ///
		(rspike diff_lb diff_ub t, ///
			lcolor(gs0)), ///
	xtitle("Years from acquisition", height(5)) xlabel(-3(1)3) ///
	ytitle("Change relative to year prior to acquisition") ///
	ylabel(#5, format(%4.1f)) xline(0, lcolor(gs10%50) lwidth(*15)) ///
	legend(order(2 "Horizontal acquisitions" "above HSR threshold" ///
				 1 "Horizontal acquisitions" "below HSR threshold") cols(1) ///
			   ring(0) bplacement(7) height(23) region(color(none)))
graph export "${paperdir}\Figures\Figure OA9b.pdf", as(pdf) replace


////////////////////////////////////////////
////									////
////	SECTION 5. Portfolio analysis 	////
////									////
////////////////////////////////////////////

* Effect of cross-market acquisitions by larger company on net price, volume, coverage
mergerPaperFigure "Figure OA10a"	/// figure tag
				  "sales" /// data
				  "w_log_net"	/// yVar
				  "(valeant_ind == 0 & cross_market == 1)"	/// sample selection rules
				  "3"	/// span
				  "YES" // graph

mergerPaperFigure "Figure OA11a"	/// figure tag
				  "sales" /// data
				  "log_units"	/// yVar
				  "(valeant_ind == 0 & cross_market == 1)"	/// sample selection rules
				  "3"	/// span
				  "YES" // graph

mergerPaperFigure "Figure OA11b"	/// figure tag
				  "coverage" /// data
				  "glp"	/// yVar
				  "(valeant_ind == 0 & cross_market == 1)"	/// sample selection rules
				  "3"	/// span
				  "YES" // graph

* acquisition of a blockbuster
mergerPaperFigure "Figure OA12a"	/// figure tag
				  "sales" /// data
				  "log_net"	/// yVar
				  "(valeant_ind == 0 & bb_acq == 1 & placebo == 0 & overlap == 0)"	/// sample selection rules
				  "3"	/// span
				  "YES"	// graph

mergerPaperFigure "Figure OA12b"	/// figure tag
				  "sales" /// data
				  "log_net"	/// yVar
				  "(valeant_ind == 0 & acq_by_bb == 1 & placebo == 0 & overlap == 0)"	/// sample selection rules
				  "3"	/// span
				  "YES"	// graph

mergerPaperFigure "Figure OA12c"	/// figure tag
				  "sales" /// data
				  "log_net"	/// yVar
				  "(valeant_ind == 0 & port_gain == 1 & placebo == 0 & overlap == 0)"	/// sample selection rules
				  "3"	/// span
				  "YES"	// graph

mergerPaperFigure "Figure OA12d"	/// figure tag
				  "sales" /// data
				  "log_net"	/// yVar
				  "(valeant_ind == 0 & port_loss == 1 & placebo == 0 & overlap == 0)"	/// sample selection rules
				  "3"	/// span
				  "YES"	// graph
